package com.lt.flashcard.view;

import java.util.List;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Path;
import org.zkoss.zk.ui.event.MouseEvent;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.A;
import org.zkoss.zul.Paging;
import org.zkoss.zul.event.PagingEvent;

import com.lt.flashcard.model.entity.FlashcardSet;

public class V_wordlist_list extends SelectorComposer<Component> {

	private static final long serialVersionUID = 1L;
	
	@Wire(".sorter")
	private List<A> lstSorter;
	
	private WordlistStartCtrl ctrl = null;
	
	@Wire
	Paging pager;
	
	@Override
	public void doBeforeComposeChildren(Component comp) throws Exception {
		Component com = Path.getComponent("//page_wordlist/ctnWordlistParent");
		ctrl = (WordlistStartCtrl) com.getAttribute("$composer");
		super.doBeforeComposeChildren(comp);
	}
	
	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		
		String sorter = (String) Executions.getCurrent().getAttribute("sorter");
		if(sorter != null) {
			A sort = (A) comp.query("#" + sorter);
			if(sort != null) {
				sort.setSclass("sorter active");
			}
		}
		
		pager.setActivePage(getCurpage());
	}

	public List<FlashcardSet> getLstFlashcardSet() {
		return ctrl.getLstFlashcardSet();
	}
	
	public int getTotal() {
		return ctrl.getTotal();
	}
	
	public int getCurpage() {
		return ctrl.getCurpage();
	}
	
	@Listen("onClick=.sorter")
	public void doSort(MouseEvent event) {
		//clear active
		for(A a : lstSorter) {
			a.setSclass("sorter");
		}
		
		A target = (A) event.getTarget();
		target.setSclass("sorter active");
		
		//notify parent to update
		ctrl.updateSorter(target.getId());
	}
	
	@Listen("onPaging=#pager")
	public void onPaging(PagingEvent event) {
		ctrl.updatePaging(event.getActivePage());
	}
}
